﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Admin
{
    public partial class Operator_Power : System.Web.UI.Page
    {
        protected string js="";
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = Request.QueryString["uid"];
            if (string.IsNullOrEmpty(uid))
            {
                Response.End();
            }
            else
            {
                uid = uid.Replace("'", "''");
            }
            if (!this.IsPostBack)
            {
                LoadProfile(uid);
            }
        }
        private void LoadProfile(string uid)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select [姓名],[部门ID],[权限],[功能],[部门编码] from [操作员] where [工号]='" + uid + "'";
            SqlDataReader Dr = Cmd.ExecuteReader();
            if (Dr.Read())
            {
                js = "var opid='" + uid + "';\n";
                js += "var opname='" + Dr.GetString(0) + "';\n";
                js += "var opdid='" + Dr.GetInt32(1).ToString() + "';\ndocument.getElementById('namemsg').innerHTML='姓名：'+opname+' 工号：'+opid+' ';\n";
                js += "var oppower='" + Dr.GetString(2) + "';\n";
                js += "var opfun='" + Dr.GetString(3) + "';\n";
                js += "var bumen='" + Dr.GetString(4) + "';\n";
                js += "var flist='";
                Dr.Close();
                Cmd.CommandText = "select [id],[名称],[分类] from [系统功能] order by [分类],[名称]";
                Dr = Cmd.ExecuteReader();
                while (Dr.Read())
                {
                    js += Dr.GetInt32(0).ToString() + "," + Dr.GetString(1) + "," + Dr.GetString(2) + "\\n";
                }
                js += "';";
            }
            else
            {
                js = "alert('用户不存在');history.back()";
            }
            Dr.Close();
            Dr.Dispose();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
        }
        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            string uid = Request.QueryString["uid"];
            if (string.IsNullOrEmpty(uid))
            {
                Response.End();
            }
            else
            {
                uid = uid.Replace("'", "''");
            }
            string fenchang;
            fenchang = Request.Form["FenChang"];
            fenchang = fenchang.Replace("'", "");
            char[] m = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
            int i;
            int t;
            string k;
            string flist = ",";
            for (i = 0; i < Request.Form.AllKeys.Length; i++)
            {
                k = Request.Form.AllKeys[i];
                if(k.StartsWith("m") && int.TryParse(k.Substring(1),out t))
                {
                    if (t <= 20 && t>0)
                    {
                        m[t - 1] = '1';
                    }
                }
                else if (Request.Form.AllKeys[i].StartsWith("fun") && int.TryParse(k.Substring(3), out t))
                {
                    flist += t.ToString() + ",";
                }
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "update [操作员] set [部门ID]=" + fenchang + ",[权限]='" + new string(m) + "',[功能]='" + flist + "',部门编码='"+this.BuMen.Text+"' where [工号]='"+uid+"'";
            Cmd.ExecuteNonQuery();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();

            this.Message.Text = "·保存成功";
            LoadProfile(uid);
        }
    }
}
